namespace MagicApp.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System.IO;
    using MagicApp.Models;
    using System.Collections.Generic;

    internal sealed class Configuration : DbMigrationsConfiguration<MagicApp.Models.CardDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            ContextKey = "MagicApp.Models.CardDbContext";
        }

        protected override void Seed(MagicApp.Models.CardDbContext context)
        {
            if (System.Diagnostics.Debugger.IsAttached == false)
                System.Diagnostics.Debugger.Launch();

            List<Card> cards = new List<Card>();
            TextReader textReader = File.OpenText(AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/AllCards.json");
            //JsonTextReader jsonReader = new JsonTextReader(textReader);
            
            JToken jsonObjects = JToken.Parse(textReader.ReadToEnd());

            CardJson newCard = jsonObjects.First.First.ToObject<CardJson>();

            bool firstRun = true;

            //This block of code converts the card from a json readable format to a SQL readable formate, converting list to strings;
            while(newCard != null)
            {
                Card cardConv = new Card();

                cardConv.name = newCard.name;
                cardConv.manaCost = newCard.manaCost;
                cardConv.convManaCost = newCard.cmc;
                cardConv.type = newCard.type;
                cardConv.rarity = newCard.rarity;
                cardConv.text = newCard.text;
                cardConv.flavor = newCard.flavor;
                cardConv.artist = newCard.artist;
                cardConv.number = newCard.number;
                cardConv.power = newCard.power;
                cardConv.toughness = newCard.toughness;
                cardConv.layout = newCard.layout;
                cardConv.multiverseID = newCard.multiverseid;
                cardConv.imageName = newCard.imageName;
                cardConv.id = newCard.id;

                try
                {
                    foreach (string x in newCard.colors)
                    {
                        cardConv.colors += x + " ";
                    }
                }
                catch{
                    cardConv.colors = "Colorless";
                }
                

                
                try
                {
                    foreach (string x in newCard.supertypes)
                    {
                            cardConv.supertype += x;
                    }
                }
                catch{
                    cardConv.supertype += "";
                }
               
                try
                {
                    foreach(string x in newCard.types)
                    {
                        cardConv.types += x + " ";
                    }
                }
                catch{
                    cardConv.types = "None";
                }

                try
                {
                     foreach(string x in newCard.subtypes)
                    {
                        cardConv.subtypes += x + " ";
                    }
                }catch{
                    cardConv.subtypes = "None";
                }   

                cards.Add(cardConv);
                if (firstRun)
                {
                    jsonObjects = jsonObjects.First.Next;
                    firstRun = false;
                }
                else
                    jsonObjects = jsonObjects.Next;

                if (jsonObjects != null)
                    newCard = jsonObjects.First.ToObject<CardJson>();
                else
                    break;
                
            }

            context.Cards.AddOrUpdate<Card>(cards.ToArray());
        }
    }
}
